package com.ljl.user.controller;

import com.ljl.basic.exception.BusinessException;
import com.ljl.user.domain.Logininfo;
import com.ljl.user.dto.EmailUto;
import com.ljl.user.dto.UserDto;
import com.ljl.user.service.ILogininfoService;
import com.ljl.user.service.IUserService;
import com.ljl.user.domain.User;
import com.ljl.user.query.UserQuery;
import com.ljl.basic.util.AjaxResult;
import com.ljl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.save(user);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                userService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id")Long id) {
        return userService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<User> findAll(){
        return userService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }


    @PostMapping("/register/phone")
    public AjaxResult registerPhone(@RequestBody UserDto userDto){
        try {
            userService.registerPhone(userDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"注册失败");
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，稍后重试！");
        }
    }

    @PostMapping("/register/email")
    public AjaxResult registerEmail(@RequestBody EmailUto emailUto){
        try {
            userService.registerEmail(emailUto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"注册失败");
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，稍后重试！");
        }
    }
    @GetMapping("/active/{id}")
    public ModelAndView active(@PathVariable("id")Long id){
        User user = userService.findById(id);
        user.setState(1);
        userService.update(user);
        Logininfo logininfo = logininfoService.findById(user.getLogininfo_id());
        logininfo.setDisable(true);
        logininfoService.update(logininfo);
        ModelAndView modelAndView = new ModelAndView("redirect:http://localhost:8081/#/login");
        return modelAndView;
    }
}
